<?php
declare (strict_types = 1);
/**
 * 素材模型
 */
namespace app\common\model;

class MatType extends BaseModel
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 't_mat_type';

    /**
     * 素材列表
     */
    public function MatTypeTree($list = [], $root = 0, $id= 'mat_type_id', $pid = 'parent_type', $child = 'children') {
        $tree = [];
        if($list){
            $refer = [];
            foreach($list as $key => $v){
                $refer[$v[$id]] =& $list[$key];
            }

            foreach($list as &$w) {
                if($w[$pid] != 0) {
                    $refer[$w[$id]]['parent_type_name'] = $refer[$w[$pid]]['mat_type_name'];
                    $refer[$w[$id]]['oper_rights'] = $refer[$w[$id]]['oper_rights'] ? explode(',', $refer[$w[$id]]['oper_rights']) : [];
                }                
            }

            foreach($list as $key => $v){
                $parentid = $v[$pid];
                if($root == $parentid) {
                    $tree[] =& $list[$key];                 
                }else{
                    if(isset($refer[$parentid])){
                        $parent =& $refer[$parentid];
                        $parent[$child][] =& $list[$key];
                    }
                }
            }
        }
        return $tree;
    }
}
